package com.syj.collect.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.syj.collect.app.R;
import com.syj.collect.entry.KeyvalueBean;
import com.syj.collect.listnner.MyItemListener;
import com.syj.collect.utils.LogUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

/**
 * created by XXX on 2021/3/4.
 */
public class AdapterWaterAnalyze extends RecyclerView.Adapter<AdapterWaterAnalyze.AdapterWaterHolder> {

    private String[] title = {"COD", "溶解氧", "阴离子", "总磷", "氨氮"};
    private MyItemListener click;
    private List<KeyvalueBean> kList = new ArrayList<>();
    private int type = 0;

    public AdapterWaterAnalyze(int type) {
        this.type = type;
        for (int i = 0; i < 5; i++) {
            KeyvalueBean bean = new KeyvalueBean(title[i]);
            kList.add(bean);
        }
    }

    public AdapterWaterAnalyze() {
        for (int i = 0; i < 5; i++) {
            KeyvalueBean bean = new KeyvalueBean(title[i]);
            kList.add(bean);
        }
    }

    @NonNull
    @Override
    public AdapterWaterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_wateranalyz_item, parent, false);
        return new AdapterWaterHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterWaterHolder holder, int position) {
        if (position == 5) {
            holder.edit.setVisibility(View.GONE);
            holder.tv.setVisibility(View.GONE);
            if (type == 0) holder.img.setVisibility(View.VISIBLE);
            else holder.img.setVisibility(View.INVISIBLE);

        } else {
            holder.edit.setVisibility(View.VISIBLE);
            holder.tv.setVisibility(View.VISIBLE);
            holder.tv.setText(title[position]);
            if (kList.get(position).getValue() > 0)
                holder.edit.setText(kList.get(position).getValue() + "");
            if (type == 0) holder.edit.setEnabled(true);
            else holder.edit.setEnabled(false);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(holder.itemView.getContext(), position + "", Toast.LENGTH_LONG).show();
                if (click != null) click.onmListenner(position);
            }
        });


        holder.edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (check(holder.edit.getText().toString())) {

                    try{
                        kList.get(position).setValue(Float.parseFloat(holder.edit.getText().toString()));
                    }catch (Exception e){
                        Toast.makeText(holder.itemView.getContext(),"必須为数字",Toast.LENGTH_LONG).show();
                    }
                }

            }
        });

    }

    private boolean check(String toString) {
        if (toString != null && toString != "" && !toString.isEmpty()) return true;
        return false;
    }

    /* //第一步 定义接口
     public interface OnItemClickListener {
         void onClick(int position);
     }

     private OnItemClickListener listener;

     //第二步， 写一个公共的方法
     public void setOnItemClickListener(OnItemClickListener listener) {
         this.listener = listener;
     }*/
    @Override
    public int getItemCount() {
        return title.length + 1;
    }

    public void setmDate(int pos, float con) {
        kList.get(pos).setValue(con);
        notifyItemChanged(pos);
    }

    public void setonclik(MyItemListener click) {
        this.click = click;
    }

    public List<KeyvalueBean> getDate() {
        return kList;
    }

    public void upData(List<KeyvalueBean> list) {
        this.kList.clear();
        this.kList.addAll(list);
        notifyDataSetChanged();
    }

    public static class AdapterWaterHolder extends RecyclerView.ViewHolder {
        private final EditText edit;
        private final TextView tv;
        private final ImageView img;

        public AdapterWaterHolder(@NonNull View itemView) {
            super(itemView);
            edit = (EditText) itemView.findViewById(R.id.adapter_water_edit);
            tv = (TextView) itemView.findViewById(R.id.adapter_water_tv);
            img = (ImageView) itemView.findViewById(R.id.adapter_water_img);
        }
    }
}
